package com.example.dao;

import com.example.pojo.Member;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class MemberDao {
    //查找所有会员
    public List<Member> memberList()
    {
        JdbcTemplate template=new JdbcTemplate(DataBaseUtils.getDataSource());
        String sql="SELECT * FROM member";
        return template.query(sql, new RowMapper<Member>() {
            @Override
            public Member mapRow(ResultSet resultSet, int i) throws SQLException {
                Member member=new Member();
                member.setId(resultSet.getInt(1));
                member.setName(resultSet.getString(2));
                member.setAge(resultSet.getInt(3));
                member.setSex(resultSet.getString(4));
                member.setPhone(resultSet.getString(5));
                member.setPass(resultSet.getString(6));
                member.setLevel(resultSet.getString(7));
                return member;
            }
        });
    }
    //增加会员(默认会员等级为普通会员)
    public Boolean addMember(Member member)
    {
        JdbcTemplate template=new JdbcTemplate(DataBaseUtils.getDataSource());
        String sql="INSERT INTO member(name,age,sex,phone,pass,level)" +
                "VALUES(?,?,?,?,?,?)";
         return template.update(sql, member.getName(),
                member.getAge(),
                member.getSex(),
                member.getPhone(),
                member.getPass(),
                "普通会员")>0;
    }
    //办理会员卡
    public Boolean setMemberLevel(String phone,String level)
    {
        JdbcTemplate template=new JdbcTemplate(DataBaseUtils.getDataSource());
        String sql="UPDATE member SET level=? WHERE phone=?";
        return template.update(sql,level,phone)>0;
    }
    //修改会员密码
    public Boolean setPass(String phone,String pass)
    {
        JdbcTemplate template=new JdbcTemplate(DataBaseUtils.getDataSource());
        String sql="UPDATE member SET pass=? WHERE phone=?";
        return template.update(sql,pass,phone)>0;

    }
    //修改会员姓名
    public Boolean setMemberName(String phone,String name)
    {
        JdbcTemplate template=new JdbcTemplate(DataBaseUtils.getDataSource());
        String sql="UPDATE member SET name=? WHERE phone=?";
        return template.update(sql,name,phone)>0;
    }
    //删除会员
    public Boolean deleteMember(String phone)
    {
        JdbcTemplate template=new JdbcTemplate(DataBaseUtils.getDataSource());
        String sql="DELETE FROM member WHERE phone=?";
        return template.update(sql,phone)>0;
    }

    @Test
    public void mytest()
    {
        MemberDao obj=new MemberDao();
        /*Member member=new Member();
        member.setAge(53);
        member.setPass("111111");
        member.setPhone("15812344321");
        member.setName("李红");
        member.setSex("女");
        System.out.println(obj.addMember(member));*/
        //System.out.println(obj.setPass("15512344321","test"));
        //System.out.println(obj.setMemberLevel("15512344321","年卡会员"));
        //System.out.println(obj.setMemberName("15512344321","刘华文"));
        //System.out.println(obj.memberList());
        //System.out.println(obj.memberList());
        //删除
        //System.out.println(obj.deleteMember("15812344321"));
        //System.out.println(obj.memberList());



    }

}
